package com.example.hotel_lxy.view.calendar;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import androidx.recyclerview.widget.RecyclerView;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.utils.DateUtils;
import com.example.hotel_lxy.utils.LunarHelper;
import com.ysxsoft.common_base.utils.TimeUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by maning on 2017/5/9.
 */

public class MNCalendarVerticalItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private ArrayList<MNCalendarItemModel> mDatas;

    private LayoutInflater layoutInflater;

    private Context context;

    private Calendar currentCalendar;

    private MNCalendarVerticalAdapter adapter;

    private String now_yyy_mm_dd;
    private Date nowDate = new Date();

    //配置信息
    private MNCalendarVerticalConfig mnCalendarVerticalConfig;


    public MNCalendarVerticalItemAdapter(Context context, ArrayList<MNCalendarItemModel> mDatas, Calendar currentCalendar, MNCalendarVerticalAdapter adapter, MNCalendarVerticalConfig mnCalendarVerticalConfig) {
        this.context = context;
        this.mDatas = mDatas;
        this.currentCalendar = currentCalendar;
        this.adapter = adapter;
        this.mnCalendarVerticalConfig = mnCalendarVerticalConfig;
        layoutInflater = LayoutInflater.from(this.context);

        now_yyy_mm_dd = MNConst.sdf_yyy_MM_dd.format(nowDate);
        try {
            nowDate = MNConst.sdf_yyy_MM_dd.parse(now_yyy_mm_dd);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = layoutInflater.inflate(R.layout.mn_item_calendar_vertical_item, parent, false);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;

            MNCalendarItemModel mnCalendarItemModel = mDatas.get(position);

            Date datePosition = mnCalendarItemModel.getDate();
            Lunar lunar = mnCalendarItemModel.getLunar();

            myViewHolder.itemView.setVisibility(View.VISIBLE);
            myViewHolder.tv_small.setVisibility(View.GONE);
            myViewHolder.iv_bg.setVisibility(View.GONE);
            myViewHolder.tv_small.setText("");

            myViewHolder.tvDay.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorSolar());
            myViewHolder.tv_small.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorLunar());
            myViewHolder.tvDay.setText(String.valueOf(datePosition.getDate()));

            //不是本月的隐藏
            Date currentDate = currentCalendar.getTime();
            if (datePosition.getMonth() != currentDate.getMonth()) {
                myViewHolder.itemView.setVisibility(View.GONE);
            }
            LunarCalendar lunarCalendar = LunarCalendar.getInstance();
            //设置标题的格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(datePosition.getTime());
            lunarCalendar.set(format);
            String formast = sdfs.format(datePosition.getTime());
            String[] split = formast.split("-");
            String string = LunarHelper.SolarToLunarString(Integer.valueOf(split[0]), Integer.valueOf(split[1]), Integer.valueOf(split[2]));
            if(!lunarCalendar.getSolarFestival().equals("")){

                if(LunarHelper.Md(string)){
                    //设置公历节日
                    myViewHolder.tv_small.setText(string+"\n"+lunarCalendar.getSolarFestival());
                }else{
                    //设置公历节日
                    myViewHolder.tv_small.setText(lunarCalendar.getSolarFestival());
                }

            }else if(!lunarCalendar.getLunarFestival().equals("")){
                //设置阴历节日
                if(LunarHelper.Md(string)){
                    //设置阴历节日
                    myViewHolder.tv_small.setText(string+"\n"+lunarCalendar.getLunarFestival());
                }else{
                    //设置阴历节日
                    myViewHolder.tv_small.setText(lunarCalendar.getLunarFestival());
                }

            }else if(!lunarCalendar.getSolarTerms().equals("")){
                //设置阴历节日
                if(LunarHelper.Md(string)){
                    //设置24节气
                    myViewHolder.tv_small.setText(string+"\n"+lunarCalendar.getSolarTerms());
                }else{
                    //设置24节气
                    myViewHolder.tv_small.setText(lunarCalendar.getSolarTerms());
                }
//                myViewHolder.tv_small.setText(lunarCalendar.getSolarTerms());
            }else{
                if(lunarCalendar.getLunarDate().equals("卅十")){
                    myViewHolder.tv_small.setText("三十");
                }else{
                    //设置普通阴历日期
                    if(LunarHelper.Md(string)){
                        //设置普通阴历日期
                        myViewHolder.tv_small.setText(string);
                    }else{
                        //设置普通阴历日期
                        myViewHolder.tv_small.setText(lunarCalendar.getLunarDate());
                    }
//                    //设置普通阴历日期
//                    myViewHolder.tv_small.setText(lunarCalendar.getLunarDate());
                }

            }
            //阴历的显示
                if (mnCalendarVerticalConfig.isMnCalendar_showLunar()) {
                    myViewHolder.tv_small.setVisibility(View.VISIBLE);
//                    String lunarDayString = LunarCalendarUtils.getLunarDayString(lunar.lunarDay);
//                    myViewHolder.tv_small.setText(lunarDayString);
                } else {
                    myViewHolder.tv_small.setVisibility(View.GONE);
                }


            String s = DateUtils.timeStamp2Date(datePosition.getTime(), null);
            //小于今天的颜色变灰
            if (datePosition.getTime() < nowDate.getTime()) {
                if(mnCalendarVerticalConfig.getMnCalendar_whetherRange()==4){
                    if (DateUtils.getDayofWeek(s) != 1) {
                        if(DateUtils.getDayofWeek(s) != 7){
                            myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_616161));
                        }else {
                            myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_F8525E));
                        }
                    } else {
                        myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_F8525E));
                    }
                    myViewHolder.tv_small.setTextColor(context.getResources().getColor(R.color.color_575656));
                }else if(mnCalendarVerticalConfig.getMnCalendar_whetherRange()==3){
                    if (DateUtils.getDayofWeek(s) != 1) {
                        if(DateUtils.getDayofWeek(s) != 7){
                            myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_616161));
                        }else {
                            myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_F8525E));
                        }
                    } else {
                        myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_F8525E));
                    }
                    myViewHolder.tv_small.setTextColor(context.getResources().getColor(R.color.color_575656));
                }
                else{
                    if (DateUtils.getDayofWeek(s) != 1 ) {
                        if( DateUtils.getDayofWeek(s) != 7){
                            myViewHolder.tvDay.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorBeforeToday());
                        }else{
                            myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_FCDCDE));
                        }
                    } else {
                        myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_FCDCDE));
                    }
                    myViewHolder.tv_small.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorBeforeToday());
                }

            }
            else{
                if(mnCalendarVerticalConfig.getMnCalendar_whetherRange()==4){
                    if (DateUtils.getDayofWeek(s) != 1 ) {
                        if( DateUtils.getDayofWeek(s) != 7){
                            myViewHolder.tvDay.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorBeforeToday());
                        }else{
                            myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_FCDCDE));
                        }
                    } else {
                        myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_FCDCDE));
                    }
                    myViewHolder.tv_small.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorBeforeToday());
                }else if(mnCalendarVerticalConfig.getMnCalendar_whetherRange()==3){
                    if (DateUtils.getDayofWeek(s) != 1) {
                        if(DateUtils.getDayofWeek(s) != 7){
                            myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_616161));
                        }else {
                            myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_F8525E));
                        }
                    } else {
                        myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_F8525E));
                    }
                    myViewHolder.tv_small.setTextColor(context.getResources().getColor(R.color.color_575656));
                }
                else{
                    if (DateUtils.getDayofWeek(s) != 1) {
                        if(DateUtils.getDayofWeek(s) != 7){
                            myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_616161));
                        }else {
                            myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_F8525E));
                        }
                    } else {
                        myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.color_F8525E));
                    }
                    myViewHolder.tv_small.setTextColor(context.getResources().getColor(R.color.color_575656));

                }


            }

            //判断是不是当天
            String position_yyy_MM_dd = MNConst.sdf_yyy_MM_dd.format(datePosition);
            if (now_yyy_mm_dd.equals(position_yyy_MM_dd)) {
                if (adapter.startDate == null) {
                    myViewHolder.tv_small.setText("今天");
                    myViewHolder.iv_bg.setVisibility(View.VISIBLE);
                    myViewHolder.iv_bg.setBackgroundResource(R.drawable.mn_selected_bg_centre);
                    myViewHolder.tvDay.setTextColor(context.getResources().getColor(R.color.colorWhite));
                    myViewHolder.tv_small.setTextColor(context.getResources().getColor(R.color.colorWhite));
//                    //动态修改颜色
                    GradientDrawable myGrad = (GradientDrawable) myViewHolder.iv_bg.getBackground();
                    myGrad.setColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeBg());
                }
            }
            if(mnCalendarVerticalConfig.getMnCalendar_whetherRange()==2){
                            //判断是不是点击了起始日期
            if (adapter.startDate != null && adapter.startDate == datePosition) {
                myViewHolder.iv_bg.setVisibility(View.VISIBLE);
                myViewHolder.iv_bg.setBackgroundResource(R.drawable.mn_selected_bg_end);
                myViewHolder.tv_small.setVisibility(View.VISIBLE);
                myViewHolder.tv_small.setText("开始");
                myViewHolder.tvDay.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
                myViewHolder.tv_small.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
                //动态修改颜色
                GradientDrawable myGrad = (GradientDrawable) myViewHolder.iv_bg.getBackground();
                myGrad.setColor(mnCalendarVerticalConfig.getMnCalendar_colorStartAndEndBg());
            }
            if (adapter.endDate != null && adapter.endDate == datePosition) {
                myViewHolder.iv_bg.setVisibility(View.VISIBLE);
                myViewHolder.iv_bg.setBackgroundResource(R.drawable.mn_selected_bg_end);
                myViewHolder.tv_small.setVisibility(View.VISIBLE);
                myViewHolder.tv_small.setText("结束");
                myViewHolder.tvDay.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
                myViewHolder.tv_small.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
                //动态修改颜色
                GradientDrawable myGrad = (GradientDrawable) myViewHolder.iv_bg.getBackground();
                myGrad.setColor(mnCalendarVerticalConfig.getMnCalendar_colorStartAndEndBg());
            }

            //判断是不是大于起始日期
            if (adapter.startDate != null && adapter.endDate != null) {
                if (datePosition.getTime() > adapter.startDate.getTime() && datePosition.getTime() < adapter.endDate.getTime()) {
                    myViewHolder.iv_bg.setVisibility(View.VISIBLE);
                    myViewHolder.iv_bg.setBackgroundResource(R.drawable.mn_selected_bg_centre);
                    myViewHolder.tvDay.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
                    myViewHolder.tv_small.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
                    //动态修改颜色
                    GradientDrawable myGrad = (GradientDrawable) myViewHolder.iv_bg.getBackground();
                    myGrad.setColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeBg());
                }
            }
            }

            //            //判断是不是大于起始日期
            if (adapter.startDate != null) {
                if (datePosition.getTime() == adapter.startDate.getTime()) {
                    myViewHolder.iv_bg.setVisibility(View.VISIBLE);
                    myViewHolder.iv_bg.setBackgroundResource(R.drawable.mn_selected_bg_centre);
                    myViewHolder.tvDay.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
                    myViewHolder.tv_small.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
                    //动态修改颜色
                    GradientDrawable myGrad = (GradientDrawable) myViewHolder.iv_bg.getBackground();
                    myGrad.setColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeBg());
                }
            }

            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MNCalendarItemModel mnCalendarItemModel = mDatas.get(position);
                    Date dateClick = mnCalendarItemModel.getDate();
                    if(mnCalendarVerticalConfig.getMnCalendar_whetherRange()==1||mnCalendarVerticalConfig.getMnCalendar_whetherRange()==3||
                            mnCalendarVerticalConfig.getMnCalendar_whetherRange()==4){
                        //必须大于今天
                        if(mnCalendarVerticalConfig.getMnCalendar_whetherRange()==1){
                            if (dateClick.getTime() < nowDate.getTime()) {
                                Toast.makeText(context, "选择的日期必须大于今天", Toast.LENGTH_SHORT).show();
                                return;
                            }
                        }else if(mnCalendarVerticalConfig.getMnCalendar_whetherRange()==4){ //必须小于今天
                            String position_yyy_MM_dd = MNConst.sdf_yyy_MM_dd.format(datePosition);
                            if(!now_yyy_mm_dd.equals(position_yyy_MM_dd)){
                                if (dateClick.getTime() >= nowDate.getTime()) {
                                    Toast.makeText(context, "选择的日期必须小于等于今天", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        }
                        if (adapter.startDate != null ) {
                            adapter.startDate = null;
                            adapter.endDate = null;
                        }
                    }
                    else{
                        if (adapter.startDate != null && adapter.endDate != null) {
                            adapter.startDate = null;
                            adapter.endDate = null;
                        }
                    }
                    if (adapter.startDate == null) {
                        adapter.startDate = dateClick;
                    } else {
                        //判断结束位置是不是大于开始位置
                        long deteClickTime = dateClick.getTime();
                        long deteStartTime = adapter.startDate.getTime();
                        if (deteClickTime <= deteStartTime) {
                            adapter.startDate = dateClick;
                        } else {
                            adapter.endDate = dateClick;
                        }
                    }
                    //选取通知
                    adapter.notifyChoose();
                    //刷新
                    notifyDataSetChanged();
                    adapter.notifyDataSetChanged();
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    private static class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView tvDay;
        private TextView tv_small;
        private ImageView iv_bg;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvDay = (TextView) itemView.findViewById(R.id.tvDay);
            tv_small = (TextView) itemView.findViewById(R.id.tv_small);
            iv_bg = (ImageView) itemView.findViewById(R.id.iv_bg);
        }

    }

}
